package dao

import (
	"context"
	"gorm.io/gorm"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
	"live/model"
)

type LiveRoomDao struct {
	conn *gorms.GormConn
}

func NewLiveRoomDao(db *db.ListenDB) *LiveRoomDao {
	return &LiveRoomDao{
		conn: gorms.New(db.Conn),
	}
}
func (l *LiveRoomDao) FindRoomByUserId(ctx context.Context, userId int64) (room *model.LiveRoom, err error) {
	session := l.conn.Session(ctx)
	//now := time.Now()
	err = session.Model(&model.LiveRoom{}).Where("user_id = ? and expire_time > now()", userId).First(&room).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return room, nil
}
